package com.mogist.hqc.module.product.ui

import android.content.Intent
import android.view.View
import butterknife.OnClick
import butterknife.Optional
import com.mogist.hqc.R
import com.mogist.hqc.base.BaseMVPActivity
import com.mogist.hqc.entitys.BaseModel
import com.mogist.hqc.module.product.contract.ConsultContract
import com.mogist.hqc.module.product.presenter.ConsultPresenter
import com.mogist.hqc.mvp.HttpDto
import com.mogist.hqc.util.Constant
import kotlinx.android.synthetic.main.activity_consult.*
import talex.zsw.basecore.util.RegTool
import talex.zsw.basecore.view.dialog.sweetalertdialog.SweetAlertDialog
import talex.zsw.basecore.view.other.RxToast

/**
 * 作用：咨询
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class ConsultActivity : BaseMVPActivity<ConsultContract.Presenter>(), ConsultContract.View
{
    private var id = ""
    override fun initArgs(intent: Intent)
    {
        intent.getStringExtra("id")
                ?.let {
                    id = it
                }
    }

    override fun initView()
    {
        setContentView(R.layout.activity_consult)
        mPresenter = ConsultPresenter(this)
    }

    override fun initData()
    {
    }

    @Optional
    @OnClick(R.id.mTvCancel, R.id.mTvSure)
    fun onViewClicked(view: View)
    {
        if (!isFastClick)
        {
            when (view.id)
            {
                R.id.mTvCancel ->
                {
                    exit()
                }
                R.id.mTvSure   ->
                {
                    val content =
                            mEtContent.text.toString()
                                    .trim()
                    if (RegTool.isEmpty(content))
                    {
                        RxToast.error("请输入咨询内容")
                    }
                    else
                    {
                        val body = BaseModel(true)
                        body.pdtId = id
                        body.remark = content
                        mPresenter.getData(HttpDto(Constant.PRODUCT_CONSULT,
                                                   body).setErrorTitle("提交咨询信息失败"))
                    }
                }
            }
        }
    }

    override fun onBackPressed()
    {
        exit()
    }

    private fun exit()
    {
        val content =
                mEtContent.text.toString()
                        .trim()
        if (RegTool.isEmpty(content))
        {
            finish()
        }
        else
        {
            showDialog(SweetAlertDialog.WARNING_TYPE,
                       "是否确定要放弃咨询？",
                       "",
                       "确定",
                       "取消",
                       finishListener,
                       null)
        }
    }

    override fun consultSuccess()
    {
        disDialog()
        RxToast.success("咨询成功，请耐心等待回复")
        finish()
    }

}